﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using SoftSource.Common;
using SoftSource.Timecard.Views;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.Controls
{
    public partial class EntryGroupControl : UserControl
    {
        public EntryGroupControl()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(EntryGroupControl_Loaded);
        }

        void EntryGroupControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.EntrySummaries != null && this.EntrySummaries.Count() > 0)
            {
                DateTime weekStart = this.EntrySummaries.First().Date.StartOfWeek(DayOfWeek.Monday);

                foreach (var summary in this.EntrySummaries)
                {
                    var difference = summary.Date - weekStart;

                    var r = new EntrySummaryView();
                    r.Date = summary.Date;
                    r.Hours = summary.Hours;

                    r.Width = 20;
                    r.Height = 20;
                    r.SetValue(Grid.ColumnProperty, difference.Days);

                    this.SummaryGrid.Children.Add(r);
                }
            }
        }

        public IEnumerable<EntrySummary> EntrySummaries
        {
            get { return this.DataContext as IEnumerable<EntrySummary>; }
        }
    }
}
